
package system.base;

import framework.core.*;

/*
	Author    :Wing
	CreateTime:2008-02-20 17:44:59
*/
public class GetStatisticSearchEvent extends SearchEvent
{

    private String[] m_warehouse_codes;
    
    private String m_get_customer_code = "";

    private String m_out_type = "" ;

    private String m_balance_flag = "" ;

    private String m_start_outTime = "";

    private String m_end_outTime = "";

    private String m_is_normal = "";

    private String m_balance_mode = "";
    
    private String m_position_code="";
    
    private String m_keeper_staff_id = "" ;
    
    private String m_status = "" ; // all
    
    
	public String getStatus()
	{
		return m_status ;
	}
	public void setStatus(String v)
	{
		m_status = v;
	}

    public String getKeeperStaffId()
    {
        return m_keeper_staff_id ;
    }
    public void setKeeperStaffId(String v)
    {
        m_keeper_staff_id = v;
    }
public GetStatisticSearchEvent() 
{
	super();
}
    
    public String[] getWarehouseCodes()
    {
        return m_warehouse_codes ;
    }
    public void setWarehouseCodes(String[] warehouseCodes)
    {
        this.m_warehouse_codes = warehouseCodes;
    }
    
    public String getGetCustomerCode()
    {
        return m_get_customer_code;
    }
    public void  setGetCustomerCode(String getCustomerCode)
    {
        this.m_get_customer_code = getCustomerCode ;
    }
    
    public String getOutType()
    {
        return m_out_type ;
    }
    public void setOutType(String v)
    {
        m_out_type = v;
    }
    
    public String getBalanceFlag()
    {
        return m_balance_flag ;
    }
    public void setBalanceFlag(String v)
    {
        m_balance_flag = v;
    }
    
    public String getStartOutTime() {
        return this.m_start_outTime;
    }
    public void setStartOutTime(String startOutTime) {
        this.m_start_outTime = startOutTime;
    }
    
    public String getEndOutTime() {
        return this.m_end_outTime;
    }
    public void setEndOutTime(String endOutTime) {
        this.m_end_outTime = endOutTime;
    }
    
    public String getIsNormal() {
        return this.m_is_normal;
    }
    public void setIsNormal(String isNormal) {
        this.m_is_normal = isNormal;
    }
    
    public String getBalanceMode() {
        return this.m_balance_mode;
    }
    public void setBalanceMode(String balanceMode) {
        this.m_balance_mode = balanceMode;
    }	
/*	
	//add by Wing start
	if (getWarehouseCodes() != null) {
        c.add(OutHead.WAREHOUSE_CODE,Conditions.IN,getWarehouseCodes());
    }
    if (!getBalanceMode().equalsIgnoreCase(""))
    {
        c.add(OutHead.ID + " in (select out_bill_id from t_out_sub where balance_mode = ?)",Conditions.CUSTOM,getBalanceMode());
    }
    if (!getIsNormal().equalsIgnoreCase(""))
    {
        c.add(OutHead.ID + " in (select out_bill_id from t_out_sub a,t_product b where a.product_code = b.CONCENTRICITY_CODE and b.is_normal = ?)",Conditions.CUSTOM,getIsNormal());
    }
    
    if (!getStartOutTime().equalsIgnoreCase("")) {
        c.add(" out_time >= ?",Conditions.CUSTOM,getStartOutTime()+" 00:00:00");
    }
    
    if (!getEndOutTime().equalsIgnoreCase("")) {
        c.add(" out_time <= ?",Conditions.CUSTOM,getEndOutTime()+" 23:59:59");
    }
    //add by Wing end
*/	
    
    //add by Jackey start    
    private String m_warehouse_code_user = "" ; // all
    
    public String getWarehouseCodeUser()
    {
        return m_warehouse_code_user ;
    }
    public void setWarehouseCodeUser(String v)
    {
        m_warehouse_code_user = v;
    }
    public String viewWarehouseCodeUser() throws Exception
    {
        return Value.view(getWarehouseCodeUser());
    }
  //add by Jackey end    
    private String[] m_warehouse_tree_codes;
    
    public String[] getWarehouseTreeCodes(){
    	return m_warehouse_tree_codes;
    }
    
    public void setWarehouseTreeCodes(String[] warehouseTreeCodes){
    	m_warehouse_tree_codes = warehouseTreeCodes;
    }

	public String getPositionCode() {
		return m_position_code;
	}

	public void setPositionCode(String m_position_code) {
		this.m_position_code = m_position_code;
	}
	
	  private String m_key_word = "";
	    public String getKeyWord()
	    {
	        return m_key_word;
	    }
	    public void setKeyWord(String keyWord)
	    {
	        this.m_key_word = keyWord;
	    }
	
	
	public Conditions toConditions() throws Exception
	{
		Conditions c = super.toConditions();

	c.add(getCondition(OutSub.POSITION_CODE,getPositionCode()));
	
	c.add(getCondition(OutSub.BALANCE_MODE,getBalanceMode()));

	c.add(getCondition(OutSub.GET_CUSTOMER_CODE,getGetCustomerCode()));
	c.add(getCondition(OutSub.STATUS,1));

		
//add by Sindy start
	String searchSQL = "select id from t_out_head where 1=1";
	boolean searchSQLFlag = false; //标示searchSQL查询是否需要执行
	String searchSQL1 = "select out_bill_id id from t_out_sub a,t_product b where a.product_code = b.CONCENTRICITY_CODE ";
	boolean searchSQLFlag1 = false; //标示searchSQL1查询是否需要执行
	
	   if (getWarehouseCodes() != null) {
	       String warehouse = "";
	       
	       for(int i=0;i<getWarehouseCodes().length;i++){
	           if(i == getWarehouseCodes().length - 1){
	               warehouse = warehouse + "warehouse_code = '"+ getWarehouseCodes()[i] +"'";
	           }else{
	               warehouse = warehouse + "warehouse_code = '"+ getWarehouseCodes()[i] +"' or ";
	           }
	       }
	       //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where "+ warehouse +")") ;
	       searchSQL +=  " and ("+ warehouse + ")";
	       searchSQLFlag = true;
	    }
	   
	   String[] searchSQLKeyWord = new String[50];
	   for(int i = 0; i < 50; i++){
	       searchSQLKeyWord[i] = "";
	   }
	   int keyCount = 0;
	   
	   String[] keyWord = new String[50];
	   for(int i = 0; i < 50; i++){
           keyWord[i] = "";
       }
	   int keyCount1 = 0;
	   
	   int keyCount2 = 0;
 
        if (!getIsNormal().equalsIgnoreCase(""))
        {
            c.add(OutSub.OUT_BILL_ID + " in (select out_bill_id from t_out_sub a,t_product b where a.product_code = b.CONCENTRICITY_CODE and b.is_normal = ?)",Conditions.CUSTOM,getIsNormal());
            searchSQL1 += " and b.is_normal = ?";
            keyWord[keyCount1] = getIsNormal();
            keyCount1++;            
            searchSQLFlag1 = true;
            
            String normalSql="select c.id from t_product c where c.is_normal="+ getIsNormal();
            c.add(OutSub.PRODUCT_ID + " in (" + normalSql + ")");
	        
        }
   
       if (!getOutType().equalsIgnoreCase(""))
       {
           //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where out_type = ?)",Conditions.CUSTOM,getOutType());
           searchSQL += " and out_type = ?";
           searchSQLKeyWord[keyCount] = getOutType();
           keyCount++;
           searchSQLFlag = true;
       }
       
       if (!getPositionCode().equalsIgnoreCase(""))
       {
           //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where out_type = ?)",Conditions.CUSTOM,getOutType());
           searchSQL += " and POSITION_CODE = ?";
           searchSQLKeyWord[keyCount] = getPositionCode();
           keyCount++;
           searchSQLFlag = true;
       }   
       
       
       if (!getBalanceFlag().equalsIgnoreCase(""))
       {
           //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where balance_flag = ?)",Conditions.CUSTOM,getBalanceFlag());
           searchSQL += " and balance_flag = ?";
           searchSQLKeyWord[keyCount] = getBalanceFlag();
           keyCount++;
           searchSQLFlag = true;
       }  
//       if (!getGetCustomerCode().equalsIgnoreCase(""))
//       {
//           //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where customer_name = ?)",Conditions.CUSTOM,getCustomerName());
//           searchSQL += " and customer_name = ?";
//           searchSQLKeyWord[keyCount] = getGetCustomerCode();
//           keyCount++;
//           searchSQLFlag = true;
//       }  
        
       if (!getBalanceMode().equalsIgnoreCase(""))
       {
    	   searchSQL += " and BALANCE_MODE = ?";
           searchSQLKeyWord[keyCount] = getBalanceMode();
           keyCount++;
           searchSQLFlag = true;
       }
       
	    if (!getStartOutTime().equalsIgnoreCase("")) {
	        //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where out_time >= ? )",Conditions.CUSTOM,getStartOutTime()+" 00:00:00");
	        searchSQL += " and out_time >= ?";
	        if(getStartOutTime().length()<10)
	            searchSQLKeyWord[keyCount] = getStartOutTime()+" 00:00:00";
	        else
	            searchSQLKeyWord[keyCount] = getStartOutTime();
	        keyCount++;
	        searchSQLFlag = true;
	    }
	    
	    if (!getEndOutTime().equalsIgnoreCase("")) {
	        //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where out_time <= ? )",Conditions.CUSTOM,getEndOutTime()+" 23:59:60");
	        searchSQL += " and out_time <= ?";
	        if(getEndOutTime().length()<10)
	            searchSQLKeyWord[keyCount] = getEndOutTime()+" 23:59:60";
	        else
	            searchSQLKeyWord[keyCount] = getEndOutTime();
            keyCount++;
            searchSQLFlag = true;
	    }
	            
	    //c.add(OutSub.OUT_BILL_ID + " in (select id from t_out_head where warehouse_code like ? )",Conditions.CUSTOM,getWarehouseCodeUser()+"%");
	    searchSQL += " and warehouse_code like ?";
	    searchSQLKeyWord[keyCount] = getWarehouseCodeUser() + "%";
	    keyCount++;
        searchSQLFlag = true;
        
        String totalSql = ""; //合并searchSQL和searchSQL1查询语句
        if(searchSQLFlag == true){
            totalSql += searchSQL;
            if(searchSQLFlag1 == true){
                totalSql += " intersect ";
            }
        }
        if(searchSQLFlag1 == true){
            totalSql += searchSQL1;
        }
        
        
        if(!"".equals(totalSql)){
            //如果有查询，则生成查询条件值字符数组。
            int total = 0;
            for(int i = 0; i < searchSQLKeyWord.length; i++){
                if(!"".equals(searchSQLKeyWord[i])){
                    total++;
                }
            }
            for(int i = 0; i < keyWord.length; i++){
                if(!"".equals(keyWord[i])){
                    total++;
                }
            }
            
            String[] kw = new String[total];
            if(total > 0){
                int j = 0;
                for(int i = 0; i < searchSQLKeyWord.length; i++){
                    if(!"".equals(searchSQLKeyWord[i])){
                        kw[j] = searchSQLKeyWord[i];
                        j++;
                    }
                }
                for(int i = 0; i < keyWord.length; i++){
                    if(!"".equals(keyWord[i])){
                        kw[j] = keyWord[i];
                        j++;
                    }
                }
                
                c.add(OutSub.OUT_BILL_ID + " in (" + totalSql + ")", Conditions.CUSTOM, kw);
            }
            else{
                c.add(OutSub.OUT_BILL_ID + " in (" + totalSql + ")");
            }
        }
        
	    


	    
		return c ;
	}
}
